package com.zygh.questionnairesurvey.controller;

import com.alibaba.fastjson.JSONObject;
import com.zygh.questionnairesurvey.common.CommonResult;
import com.zygh.questionnairesurvey.entity.Problem;
import com.zygh.questionnairesurvey.server.ProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/Problem")
@Api(tags = "问题列表")
public class ProblemController {

    @Resource
    private ProblemService problemService;


    @GetMapping("list")
    @ApiOperation(value = "查询所有问题信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "Integer", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "Integer", required = true)
    })
    public CommonResult listAll(@RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return problemService.listAll(pageNum, pageSize);
    }


    @GetMapping("getById")
    @ApiOperation(value = "根据id查询问题信息")
    public CommonResult getById(Integer id) {
        return problemService.getById(id);
    }


    @PostMapping("insert")
    @ApiOperation(value = "新增问题")
    public CommonResult insert(@RequestBody Problem problem) {
        return problemService.insert(problem);
    }


    @PostMapping("update")
    @ApiOperation(value = "修改问题")
    public CommonResult update(@RequestBody Problem dept) {
        return problemService.update(dept);
    }


    @PostMapping("delete")
    @ApiOperation(value = "删除问题")
    public CommonResult delete(@RequestBody String id) {
        String str = JSONObject.parseObject(id).get("id").toString();
        Integer new_id = Integer.parseInt(str);
        return problemService.delete(new_id);
    }

    @GetMapping("findQuestionnaireByid")
    @ApiOperation(value = "根据警号查询调查问卷")
    public CommonResult findQuestionnaireByid(String username) {
        return problemService.findQuestionnaireByid(username);
    }

    @GetMapping("findQuestionnaireByDeptid")
    @ApiOperation(value = "根据部门、警号查询调查问卷")
    public CommonResult findQuestionnaireByDeptid(String username, Integer deptid,
                                                  @RequestParam(name = "pageNum", defaultValue = "1") Integer pageNum,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        return problemService.findQuestionnaireByDeptid(username, deptid, pageNum, pageSize);
    }


}
